package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.file.service.FileStorageService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.utils.wemedia.WmThreadLocalUtil;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.UUID;

@Service
public class WmMaterialServiceImpl implements WmMaterialService {

    @Autowired
    private FileStorageService fileStorageService;

    @Autowired
    private WmMaterialMapper wmMaterialMapper;

    @Override
    public ResponseResult uploadPicture(MultipartFile multipartFile) throws IOException {
        //1. 校验参数
        if (multipartFile == null) {
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        //2. 上传图片到minio
        String originalFilename = multipartFile.getOriginalFilename();
        String fileName = UUID.randomUUID().toString().replace("-","");
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        String url = fileStorageService.uploadImgFile("", fileName + suffix, multipartFile.getInputStream());

        //3. 保存素材数据到数据库
        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setUserId(WmThreadLocalUtil.getUser().getId());
        wmMaterial.setUrl(url);
        wmMaterial.setCreatedTime(new Date());
        wmMaterial.setType((short) 0);
        wmMaterial.setIsCollection((short) 0);
        wmMaterialMapper.insert(wmMaterial);

        return ResponseResult.okResult(wmMaterial);
    }

    @Override
    public ResponseResult list(WmMaterialDto wmMaterialDto) {
        //1.检查参数
        wmMaterialDto.checkParam();
        System.out.println(wmMaterialDto.getIsCollection());
        //封装请求条件
        LambdaQueryWrapper<WmMaterial> wrapper = Wrappers.lambdaQuery(WmMaterial.class);
        //判断是否为收藏
        if (wmMaterialDto.getIsCollection()!=null&&wmMaterialDto.getIsCollection()==1){
            wrapper.eq(WmMaterial::getIsCollection,wmMaterialDto.getIsCollection());
        }
        wrapper.eq(WmMaterial::getUserId,WmThreadLocalUtil.getUser().getId());

        wrapper.orderByDesc(WmMaterial::getCreatedTime);

        //发送请求
        Page page = wmMaterialMapper.selectPage(new Page(wmMaterialDto.getPage(), wmMaterialDto.getSize()), wrapper);

        //封装返回结果
        ResponseResult responseResult=new PageResponseResult(wmMaterialDto.getPage(),wmMaterialDto.getSize(), (int) page.getTotal());
        responseResult.setData(page.getRecords());
        return responseResult;
    }
}
